// pages/visitSign/visitSign.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList:[],
    startTime:'',
    endTime:'',
    nowTimeType:'',
    time_popup_show:false,
  },

  
  /**选择时间 */
  chooseTime(e){
    let type = e.currentTarget.dataset.type;
    let currentTimestamp='';
    if(type=='startTime'){
        currentTimestamp = currentTimestamp = this.data.startTime?new Date(this.data.startTime).getTime():new Date().getTime();
    }else if(type=='endTime'){
        currentTimestamp = currentTimestamp = this.data.endTime?new Date(this.data.endTime).getTime():new Date().getTime();
    }
    this.setData({
      time_popup_show:true,
      currentTimestamp:currentTimestamp,
      nowTimeType:type
    })
  },
  /**日期选择确认 */
  timeConfirm(e){
    if(this.data.nowTimeType=='startTime'){
      this.setData({
        startTime:api.formatDateCRM('Y-m-d H:i:s',e.detail),
        time_popup_show:false
      })
    }else if(this.data.nowTimeType=='endTime'){
      this.setData({
        endTime:api.formatDateCRM('Y-m-d H:i:s',e.detail),
        time_popup_show:false
      })
    }
  },
  timeCancel(e){
    this.setData({
      time_popup_show:false
    })
  },
  
  
  /**上传文件 */
  afterRead(event) {
    let that = this;
    const { file } = event.detail;

    api.uploadFileCRM(app.root+'?cla=fileUploadApp&fun=uploadFile',file,{power: 'visitPlan',fileType:'访客计划'}).then((res)=>{
      // 上传完成需要更新 fileList
           const { fileList = [] } = that.data;
           fileList.push({ ...file, url: res.result.src,id:res.result.id });
           that.setData({ fileList });
         })
  },
  /**删除文件 */
  delFile(event){
    let index = event.detail.index;
    let fileList = this.data.fileList;
    let newFileList=[];
    let id = '';
    fileList.forEach((v,i)=>{
      if(i==index){
        id=v.id;
      }else{
        newFileList.push(v);
      }
    })
  
    let that = this;
    api.curlPost('?cla=fileUploadApp&fun=delFile',{id:id,power:'visitPlan'}).then((res)=>{
      that.setData({
        fileList:newFileList
      })
    })
    
  },
  
  /**提交 */
  commit(e){
    let para = e.detail.value;
    let fileId = [];
    this.data.fileList.forEach((v,i)=>{
      fileId.push(v.id)
    })
    para.fileId = fileId.join(',');

    para.id=this.data.id;
    para.factTime=this.data.startTime+' ~ '+this.data.endTime;

    api.curlPost('?cla=clientAppClass&fun=visitPlanResultEdit',para).then((res)=>{
      wx.navigateBack({
        delta:1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let id = options.id;
    that.requestData(id);
  },

  requestData(id){
    let that = this;
    api.curlPost('?cla=clientAppClass&fun=detailVisitPlan',{id:id}).then((res)=>{
      res.data.memberName = res.data.memberName.join(',');
      that.setData({
        info:res.data,
        id:res.data.id,
        fileList:res.data.file,
        startTime:res.data.factStartTime,
        endTime:res.data.factEndTime,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})